package com.ruoyi.result.publication.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.result.patents.domain.ResultPatents;
import com.ruoyi.result.publication.domain.dto.ResultPublicationDTO;
import com.ruoyi.result.publication.domain.vo.ResultPublicationVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.team.trash.domain.AchievementTrash;
import com.ruoyi.team.trash.mapper.AchievementTrashMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.result.publication.mapper.ResultPublicationMapper;
import com.ruoyi.result.publication.domain.ResultPublication;
import com.ruoyi.result.publication.service.IResultPublicationService;

/**
 * 著作信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class ResultPublicationServiceImpl implements IResultPublicationService
{
    @Autowired
    private ResultPublicationMapper resultPublicationMapper;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AchievementTrashMapper achievementTrashMapper;

    /**
     * 查询著作信息
     *
     * @param id 著作信息主键
     * @return 著作信息
     */
    @Override
    public ResultPublication selectResultPublicationById(Long id)
    {
        return resultPublicationMapper.selectResultPublicationById(id);
    }
    /**
     * 批量获取著作信息
     */
    @Override
    public List<ResultPublicationVO> selectResultPublicationByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Collections.emptyList();
        }
        List<ResultPublication> resultPublicationList = resultPublicationMapper.selectResultPublicationByIds(ids);
        List<ResultPublicationVO> resultPublicationVOList = new ArrayList<>();

        for (ResultPublication resultPublication1 : resultPublicationList) {
            ResultPublicationVO resultPublicationVO = new ResultPublicationVO();
            // 假设 resultPatent 和 resultPublicationVO 有相同的字段，先进行复制
            BeanUtils.copyProperties(resultPublication1, resultPublicationVO);

            // 根据 userId 查询 nick_name
            SysUser sysUser = sysUserMapper.selectUserById(resultPublication1.getUserId());
            resultPublicationVO.setNickName(sysUser.getNickName());

            resultPublicationVOList.add(resultPublicationVO);
        }
        return resultPublicationVOList;
    }

    /**
     * 查询著作信息列表
     *
     * @param resultPublication 著作信息
     * @return 著作信息
     */
    @Override
    public TableDataInfo selectResultPublicationList(ResultPublicationDTO resultPublication) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptId = loginUser.getUser().getDeptId();

        // 若依默认超级管理员用户ID为1
        boolean isSuperAdmin = loginUser.getUser().getUserId().equals(1L);

        // 非超级管理员才进行部门过滤
        if (!isSuperAdmin) {
            resultPublication.setDeptId(deptId);
        }

        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (StringUtils.isNotNull(pageDomain.getPageNum()) && StringUtils.isNotNull(pageDomain.getPageSize())) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), orderBy)
                    .setReasonable(pageDomain.getReasonable());
        }

        List<ResultPublication> resultPublicationList = resultPublicationMapper.selectResultPublicationList(resultPublication);

        List<ResultPublicationVO> resultPublicationVOList = resultPublicationList.stream().map(r -> {
            ResultPublicationVO vo = new ResultPublicationVO();
            BeanUtils.copyProperties(r, vo);
            SysUser sysUser = sysUserMapper.selectUserById(r.getUserId());
            vo.setNickName(sysUser.getNickName());
            return vo;
        }).collect(Collectors.toList());

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(resultPublicationList).getTotal());
        rspData.setRows(resultPublicationVOList);
        return rspData;
    }


    /**
     * 新增著作信息
     *
     * @param resultPublication 著作信息
     * @return 结果
     */
    @Override
    public int insertResultPublication(ResultPublication resultPublication)
    {
        return resultPublicationMapper.insertResultPublication(resultPublication);
    }

    /**
     * 修改著作信息
     *
     * @param resultPublication 著作信息
     * @return 结果
     */
    @Override
    public int updateResultPublication(ResultPublication resultPublication)
    {
        return resultPublicationMapper.updateResultPublication(resultPublication);
    }

    /**
     * 批量删除著作信息
     *
     * @param ids 需要删除的著作信息主键
     * @return 结果
     */
    @Override
    public int deleteResultPublicationByIds(Long[] ids)
    {
        List<ResultPublication> resultPublicationList = resultPublicationMapper.selectResultPublicationByIds(ids);
        for (ResultPublication resultPublication : resultPublicationList) {
            AchievementTrash achievementTrash = new AchievementTrash();
            achievementTrash.setUserid(resultPublication.getUserId());
            achievementTrash.setAchievementType("著作");
            achievementTrash.setTitle(resultPublication.getTitle());
            achievementTrash.setAchievementId(resultPublication.getId());
            achievementTrash.setDeletedAt(new Date());
            achievementTrashMapper.insertAchievementTrash(achievementTrash);
        }
        return resultPublicationMapper.deleteResultPublicationByIds(ids);
    }

    /**
     * 删除著作信息信息
     *
     * @param id 著作信息主键
     * @return 结果
     */
    @Override
    public int deleteResultPublicationById(Long id)
    {
        return resultPublicationMapper.deleteResultPublicationById(id);
    }
}
